﻿using System;
using System.ComponentModel;
using System.Windows.Input;
using MSDNWebcast.Commands;
using MSDNWebcast.Models;
using MSDNWebcast.Service;

namespace MSDNWebcast.ViewModels
{
    /// <summary>
    /// 课件
    /// </summary>
    public class CourseFileViewModel : ViewModelBase, ICleanUp
    {
        #region Fields

        /// <summary>
        /// 
        /// </summary>
        private ICommand m_Command;

        /// <summary>
        /// 
        /// </summary>
        private Downloader m_Downloader;

        #endregion

        #region Constructors

        /// <summary>
        /// 课件视图
        /// </summary>
        /// <param name="fileType"></param>
        /// <param name="course"></param>
        public CourseFileViewModel(FileType fileType, Course course)
            : this(new CourseFile(course, fileType))
        {

        }

        public CourseFileViewModel(CourseFile courseFile)
        {
            CourseFile = courseFile;
            CourseFile.PropertyChanged += new PropertyChangedEventHandler(CourseFile_PropertyChanged);
        }

        #endregion

        #region Properties

        /// <summary>
        /// 所属课程
        /// </summary>
        public CourseFile CourseFile { get; private set; }

        /// <summary>
        /// 下载
        /// </summary>
        public Downloader Downloader
        {
            get
            {
                if (m_Downloader == null)
                {
                    m_Downloader = ServiceFactory.Instance.GetService<DownloadService>().GetDownloader(CourseFile);
                    if (m_Downloader != null)
                        m_Downloader.PropertyChanged += new PropertyChangedEventHandler(downloader_PropertyChanged);
                }
                return m_Downloader;
            }
        }

        /// <summary>
        /// 文件路径
        /// </summary>
        public string FilePath { get; set; }

        /// <summary>
        /// 下载状态
        /// </summary>
        public DownloadState State
        {
            get
            {
                if (CourseFile.CanOpen)
                    return DownloadState.Finished;
                else
                {
                    return this.Downloader != null ? this.Downloader.State : DownloadState.NotStarted;
                }
            }
        }

        /// <summary>
        /// 能否打开课件
        /// </summary>
        public bool CanOpen
        {
            get { return !CanPlay && CourseFile.CanOpen; }
        }

        /// <summary>
        /// 能否播放
        /// </summary>
        public bool CanPlay
        {
            get { return CourseFile.CanPlay; }
        }

        /// <summary>
        /// 是否在处理中
        /// </summary>
        public bool IsProcessing
        {
            get
            {
                if (Downloader == null) return false;
                return this.Downloader.State == DownloadState.WaitToDownload ||
                       this.Downloader.State == DownloadState.Downloading ||
                       this.Downloader.State == DownloadState.Finished && !this.CourseFile.CanOpen;
            }
        }

        /// <summary>
        /// 下载文件类型
        /// </summary>
        public string TypeName
        {
            get
            {
                return CourseFile.FileType.Name;
            }
        }

        /// <summary>
        /// 可执行操作:下载/观看
        /// </summary>
        public ICommand Command
        {
            get
            {
                if (m_Command == null)
                {
                    m_Command = new DelegateCommand(Process, CanProcess);
                }
                return m_Command;
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// 下载课件或打开课件
        /// </summary>
        private void Process()
        {
            if (this.State == DownloadState.NotStarted)
            {
                ServiceFactory.Instance.GetService<DownloadService>().DownloadCourse(CourseFile);
                OnPropertyChanged("State");
                OnPropertyChanged("IsProcessing");
            }
            else if (CanOpen)
            {
                CourseFile.Open();
            }
            else if (CanPlay)
            {
                GlobalCommands.OpenVideo.Execute(CourseFile);
            }
        }

        /// <summary>
        /// 是否可执行
        /// </summary>
        /// <returns></returns>
        private bool CanProcess()
        {
            return State != DownloadState.Downloading;
        }

        /// <summary>
        /// 下载完成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void CourseFile_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "CanOpen")
            {
                OnPropertyChanged("CanOpen");
                OnPropertyChanged("CanPlay");
                OnPropertyChanged("IsProcessing");
            }
        }

        /// <summary>
        /// 属性值改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void downloader_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "State")
            {
                OnPropertyChanged("State");
            }
        }

        #endregion

        public void CleanUp()
        {
            if (m_Downloader != null)
                m_Downloader.PropertyChanged -= new PropertyChangedEventHandler(downloader_PropertyChanged);
            CourseFile.PropertyChanged -= new PropertyChangedEventHandler(CourseFile_PropertyChanged);
        }
    }
}